package com.pubinfo.passbook.common.utils;

import cn.hutool.core.date.DateUtil;
import com.pubinfo.passbook.common.entity.TPassbook;
import com.pubinfo.passbook.common.entity.TSourceArtificial;
import com.pubinfo.passbook.common.model.ruler.PayInformation;
import org.apache.commons.collections.CollectionUtils;

import java.text.SimpleDateFormat;
import java.util.*;

public class UccpUtil {
  public static String getSubmitDate(List<TPassbook> passbookList) {
    if(CollectionUtils.isEmpty(passbookList))return DateUtil.format(new Date(),"MM月dd日");
    Collections.sort(passbookList, new Comparator<TPassbook>() {
      @Override
      public int compare(TPassbook o1, TPassbook o2) {
        return o1.getCreateTime().compareTo(o2.getCreateTime());
      }
  
    });
  
    SimpleDateFormat format = new SimpleDateFormat("MM月dd日");
    String startDate = format.format(passbookList.get(0).getCreateTime());
    String endDate = format.format(passbookList.get(passbookList.size() - 1).getCreateTime());
    String dateStr = "";
    if (startDate.equals(endDate)) {
      dateStr = startDate;
    } else {
      dateStr = startDate + "-" + endDate;
    }
    return dateStr;
  }
  
  public static String getArtificialSubmitDate(List<TSourceArtificial> artificials) {
    
    Collections.sort(artificials, new Comparator<TSourceArtificial>() {
      @Override
      public int compare(TSourceArtificial o1, TSourceArtificial o2) {
        return o1.getCreateTime().compareTo(o2.getCreateTime());
      }
    });
    SimpleDateFormat format = new SimpleDateFormat("MM月dd日");
    String startDate = format.format(artificials.get(0).getCreateTime());
    String endDate = format.format(artificials.get(artificials.size() - 1).getCreateTime());
    String dateStr = "";
    if (startDate.equals(endDate)) {
      dateStr = startDate;
    } else {
      dateStr = startDate + "-" + endDate;
    }
    return dateStr;
  }
  
  public static Integer isTimeout(List<PayInformation> payInformations) {
    Integer count = 0;
    if (!CollectionUtils.isEmpty(payInformations)) {
      for (PayInformation payInformation : payInformations) {
        if (isTimeout(payInformation.getIssueDate())) {
          count++;
        }
      }
    }
    
    return count;
  }
  
  //判断是否超时报销
  public static boolean isTimeout(Date issueDate) {
    //如果现在时间过了1.26号，则去年开票的票据不能再报销
    Calendar calendar = Calendar.getInstance();
    int march = calendar.get(Calendar.MARCH) + 1;
    int day = calendar.get(Calendar.DATE);
    int lastYear = calendar.get(Calendar.YEAR) - 1;
    if (march >= 1 && day > 26) {
      calendar.setTime(issueDate);
      //获取票据开票日期，如果是上年的票据，则不报销。
      int year = calendar.get(Calendar.YEAR);
      if (year == lastYear) {
        return true;
      }
    }
    return false;
  }
  
}
